package tq.com.tqteacher.model;

import android.app.Activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.HashMap;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQLoadingHelper;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqteacher.activity.TQUploadFinishActivity;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.propertyEnum.TQModify;

public class TQNewPublish implements Serializable {
    final static public int LINE_NULL = 0;

    public String getName() {
        return name;
    }

    public void setName(String pName) {
        name = pName;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String pDesc) {
        desc = pDesc;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String pPrice) {
        price = pPrice;
    }

    public int getShow() {
        return show;
    }

    public void setShow(int pShow) {
        show = pShow;
    }

    public int getType() {
        return type;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int pGrade) {
        grade = pGrade;
    }

    public void setType(int pType) {
        type = pType;
    }
    public int getParentId() {
        return chapter;
    }
    public void setParentId(int pParentId) {
        chapter = pParentId;
    }

    public int getChildId() {
        return childId;
    }
    public void setChildId(int pChildId) {
        childId = pChildId;
    }

    public int getSubject() {
        return subject;
    }

    public void setSubject(int pSubject) {
        subject = pSubject;
    }

    private String name;
    private String desc;
    private int grade;

    private String price;
    private int show;
    private int type;
//    private int step;
//    private int parentId = 0;
    private int childId = 0;
    private int chapter = 0;
    private int seq_id = -1;
    private int subject;

    private TQUserInfo creator = new TQUserInfo();

    public TQNewPublish(){
        subject = TQLogin.getCurrent().getUserInfo().getSubject();
        grade = TQLogin.getCurrent().getUserInfo().getGrade();

        creator.copy(TQLogin.getCurrent().getUserInfo());
    }

    public int getSeq_id() {
        return seq_id;
    }

    public void setSeq_id(int pSeq_id) {
        seq_id = pSeq_id;
    }

    public void onPulishComplete(INetCallback pNetCallback){

    }

    public void onModifyBaseInfo(INetCallback pNetCallback){

    }

    public void onNewPublishResult(JSONObject pObject){
        if(pObject == null) return;
        seq_id = pObject.optInt("seq_id");
    }

    public void fromJson(JSONObject pObject){
        if(pObject == null) return;

        seq_id = pObject.optInt("id");
        name = pObject.optString("title");
        type = pObject.optInt("category");
//        parentId = pObject.optInt("parent");
        childId = pObject.optInt("child");
        chapter = pObject.optInt("chapter");
        desc = pObject.optString("desc");
        subject = pObject.optInt("subject");

        JSONObject c = pObject.optJSONObject("creator");
        if(c != null){
            creator.refresh(c);
        }

        state = pObject.optInt("state");
        edit_times = pObject.optInt("edit_times");
        show = pObject.optInt("access_type", TQSystemDefine.SHOW_TYPE_ALL);
    }

    public void refreshAttach(JSONArray pObject){

    }

    public boolean isHeader(){
        return chapter == LINE_NULL;
    }
    public boolean hasChild(){
        return chapter != LINE_NULL;
    }

    private boolean modify = false;
    public boolean isModify(){
        return modify;
    }

    public void setModify(boolean pModify) {
        modify = pModify;
    }

    public TQUserInfo getCreator() {
        return creator;
    }

    public void setCreator(TQUserInfo pCreator) {
        creator = pCreator;
    }

    private TQCommentOverInfoModel mCommentOverInfoModel;
    private TQLookerOverInfo mLookerOverInfo;

    public TQCommentOverInfoModel getCommentOverInfoModel() {
        return mCommentOverInfoModel;
    }

    public void setCommentOverInfoModel(TQCommentOverInfoModel pCommentOverInfoModel) {
        mCommentOverInfoModel = pCommentOverInfoModel;
    }

    public TQLookerOverInfo getLookerOverInfo() {
        return mLookerOverInfo;
    }

    public void setLookerOverInfo(TQLookerOverInfo pLookerOverInfo) {
        mLookerOverInfo = pLookerOverInfo;
    }

    private boolean needLoadRemote;
    public boolean getNeedloadRemote(){
        return needLoadRemote;
    }

    public void setNeedLoadRemote(boolean pNeedLoadRemote) {
        needLoadRemote = pNeedLoadRemote;
    }

    private int state;
    private int edit_times;

    public int getState() {
        return state;
    }

    public void setState(int pState) {
        state = pState;
    }

    public int getEdit_times() {
        return edit_times;
    }

    public void setEdit_times(int pEdit_times) {
        edit_times = pEdit_times;
    }
    public void onPublish(INetCallback pNetCallback){
        HashMap<TQModify.MaterialBaseInfo, Object> info = new HashMap<>();
        info.put(TQModify.MaterialBaseInfo.state, "1");

        TQNetInterface.modifyMaterialInfo(getSeq_id(), info, pNetCallback);
    }
    public boolean isPublish(){
        return getState() == TQSystemDefine.PUBLISH_STATE_Publish;
    }

    public void getDetailFromServer(final Activity pActivity, final INetFinishListener pListener){
        TQLoadingHelper.build(pActivity).setCancelable(false).show();
        TQNetInterface.getMatrialDetail(TQPublishType.getSharePublishItem().getSeq_id(), new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                if(!pNetResult.isSuccess()){
//                    AlertDialogHelper.show(pActivity, pNetResult.getErrMsg());
                    TQLoadingHelper.setError(pNetResult.getErrMsg());
                    return;
                }

                try {
                    if(pNetResult.getData() == null) return;

                    JSONObject lObject = pNetResult.getData().optJSONObject("material");
                    if(lObject == null) return;

                    JSONObject lBaseInfo = lObject.optJSONObject("material");
                    if(lBaseInfo == null) return;

                    int cate = lBaseInfo.optInt("category");
                    if(cate != TQPublishType.getSharePublishItem().getType()){
                        return;
                    }else{
                        TQPublishType.getSharePublishItem().fromJson(lBaseInfo);
                    }

                    JSONArray lAttach = lObject.optJSONArray("attachment");
                    if(lAttach != null){
                        TQPublishType.getSharePublishItem().refreshAttach(lAttach);
                    }

                    if(pListener != null){
                        pListener.finish();
                    }
                }finally {
                    TQLoadingHelper.dimiss();
                }

            }
        });
    }

    public interface INetFinishListener{
        void finish();
    }
}
